"""使用jieba库对三国演绎的内容进行词频分析"""
import jieba
from collections import Counter

# 人物别名映射字典
name_mapping = {
    '孔明曰': '孔明',
    '孟德': '曹操',
    '孟德曰': '曹操',
    '玄德': '刘备',
    '玄德曰': '刘备',
    '关公': '关羽',
    '关公曰': '关羽',
    '云长': '关羽',
    '云长曰': '关羽'
}

# 读取文件内容
with open('三国演义.txt', 'r', encoding='utf-8') as file:
    content = file.read()

# 分词并统计词频
words = [name_mapping.get(word, word) for word in jieba.lcut(content) if len(word) > 1]
word_counts = Counter(words)

# 输出前20个高频词
for word, count in word_counts.most_common(20):
    print(f"{word}: {count}")
